import request from "@/utils/request";
import { AxiosPromise } from "axios";

import {
  QueryCategoryListParams,
  CategoryPageResult,
  CategoryTreeResult,
  AddOrUpdateCategoryParams,
} from "./types";

/**
 * 分页列表
 *
 * @param queryParams
 */
export function getCategoryPage(
  queryParams: QueryCategoryListParams
): AxiosPromise<CategoryPageResult> {
  return request({
    url: "/api/category/list",
    method: "get",
    params: queryParams,
  });
}

/**
 * 新增
 * @param data
 */
export function addCategory(data: AddOrUpdateCategoryParams) {
  return request({
    url: "/api/category/add",
    method: "post",
    data: data,
  });
}

/**
 * 修改
 * @param id
 * @param data
 */
export function updateCategory(data: AddOrUpdateCategoryParams) {
  return request({
    url: "/api/category/update",
    method: "put",
    data: data,
  });
}

/**
 * 删除
 * @param id
 */
export function deleteCategory(id: number) {
  return request({
    url: "/api/category/delete/" + id,
    method: "delete",
  });
}

/**
 * 修改状态
 */
export function updateStatus(id: number, status: number) {
  return request({
    url: `/api/category/update/status/${id}/${status}`,
    method: "put",
  });
}

/**
 * 获取分类树
 */
export function getCategoryTree(): AxiosPromise<CategoryTreeResult> {
  return request({
    url: "/api/category/tree",
    method: "get",
  });
}

/**
 * 获取某一级分类
 */
export function getCategoryByLevel(level: number) {
  return request({
    url: "/api/category/level/" + level,
    method: "get",
  });
}

/**
 * 获取分类详情
 */
export function getCategoryDetail(
  id: number
): AxiosPromise<AddOrUpdateCategoryParams> {
  return request({
    url: `/api/category/${id}`,
    method: "get",
  });
}
